package com.wrial.kind.doublepointer;
/*
 * @Author  Wrial
 * @Date Created in 17:02 2020/8/7
 * @Description
 *
 *      输入:
        s = "abpcplea", d = ["ale","apple","monkey","plea"]
        输出:
        "apple"
     如果答案不止一个，返回长度最长且字典顺序最小的字符串。如果答案不存在，则返回空字符串。

 */

import java.util.List;

public class findLongestWord {

    /**
     * 双指针，一个指向s，一个指向d中元素，条件就是s或者d中元素超出长度
     */
    public String findLongestWord(String s, List<String> d) {

        String longestAns = "";
        for (String s1 : d) {
            int ps = 0, pd = 0;
            while (ps < s.length() && pd < s1.length()) {
                if (s.charAt(ps) == s1.charAt(pd)) {
                    pd++;
                }
                ps++;
            }
            if (pd == s1.length()) {
                if (pd > longestAns.length()) longestAns = s1;
                else if (pd == longestAns.length()) {
                    longestAns = (s1.compareTo(longestAns) < 0) ? s1 : longestAns;
                }
            }
        }
        return longestAns;
    }

    public static void main(String[] args) {

        System.out.println("ab".compareTo("ac"));
    }
}
